package com.itcat.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.OmsOrderApi;
import com.itcat.api.mysql.OmsOrderItemApi;
import com.itcat.api.mysql.OmsOrderOperateHistoryApi;
import com.itcat.common.CommonPage;
import com.itcat.mysql.param.DeliveryParam;
import com.itcat.mysql.param.MoneyInfoParam;
import com.itcat.mysql.param.OmsOrderParam;
import com.itcat.mysql.param.ReceiverInfoParam;
import com.itcat.mysql.pojo.OmsOrder;
import com.itcat.mysql.pojo.OmsOrderItem;
import com.itcat.mysql.pojo.OmsOrderOperateHistory;
import com.itcat.mysql.vo.OmsOrderVo;
import com.itcat.service.OmsOrderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Component
@Service
public class OmsOrderServiceImpl implements OmsOrderService {


    @DubboReference
    private OmsOrderApi omsOrderApi;

    @DubboReference
    private OmsOrderItemApi omsOrderItemApi;

    @DubboReference
    private OmsOrderOperateHistoryApi omsOrderOperateHistoryApi;


    /**
     * 分页查询订单信息
     *
     * @param omsOrderParam
     * @return
     */
    public CommonPage OrderList(OmsOrderParam omsOrderParam) {

        Page<OmsOrder> pageResult = omsOrderApi.OrderList(omsOrderParam);

        return CommonPage.restPage(pageResult);


    }

    /**
     * 批量删除订单
     *
     * @param ids
     * @return
     */
    public void delete(Integer[] ids) {
        //批量删除订单信息
        omsOrderApi.delete(ids);

    }

    /**
     * 批量关闭订单
     *
     * @param ids
     * @param note
     */
    public void close(Integer[] ids, String note) {
        //设置需要改变的订单状态
        Integer code = 4;
        //调用omsOrderApi批量关闭订单
        omsOrderApi.close(ids, note, code);
        //关闭订单状态时也要把后台操作表的订单状态同步改变
        omsOrderOperateHistoryApi.close(ids, note, code);


    }

    /**
     * 批量发货
     *
     * @param
     * @param
     * @param
     */
    public void delivery(List<DeliveryParam> list) {
        //设置发货状态
        int code = 2;
        //调用omsOrderApi
        omsOrderApi.delivery(list, code);
        //发货订单状态时也要把后台操作表的订单状态同步改变
        omsOrderOperateHistoryApi.delivery(list, code);
    }

    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    public OmsOrderVo orderDetail(Integer id) {
        //调用omsOrderItemApi查询订单商品详情
        List<OmsOrderItem> itemList = omsOrderItemApi.findItem(id);
        //调用omsOrderOperateHistoryApi查询订单操作历史记录
        List<OmsOrderOperateHistory> historyList = omsOrderOperateHistoryApi.findHistory(id);
        //调用omsOrderApi查询订单详情
        OmsOrder omsOrder = omsOrderApi.findOrder(id);
        //将查询到的信息封装到vo对象并返回
        OmsOrderVo vo = new OmsOrderVo();
        if (omsOrder != null) {
            BeanUtils.copyProperties(omsOrder, vo);
        }
        if (itemList != null) {
            vo.setOrderItemList(itemList);
        }
        if (historyList != null) {
            vo.setHistoryList(historyList);
        }
        return vo;
    }

    /**
     * 订单备注
     *
     * @param id
     * @param note
     * @param status
     */
    public void note(Integer id, String note, Integer status) {
        //调用Api修改订单备注信息
        omsOrderApi.updateNote(id, note, status);
    }

    /**
     * 修改收货人信息
     *
     * @param receiverInfoParam
     */
    public void receiverInfo(ReceiverInfoParam receiverInfoParam) {
        OmsOrder omsOrder = new OmsOrder();
        if(receiverInfoParam!=null){
            BeanUtils.copyProperties(receiverInfoParam, omsOrder);
        }

        //将修改时间封装到对象
        omsOrder.setModifyTime(new DateTime(System.currentTimeMillis()));
        omsOrder.setId(Long.valueOf(String.valueOf(receiverInfoParam.getOrderId())));
        omsOrderApi.updateReceiverInfo(omsOrder);
    }

    /**
     * 修改订单费用信息
     *
     * @param moneyInfoParam
     */
    public void moneyInfo(MoneyInfoParam moneyInfoParam) {
        OmsOrder omsOrder = new OmsOrder();
        if(moneyInfoParam!=null){
            BeanUtils.copyProperties(moneyInfoParam, omsOrder);
        }
        //将修改时间封装到对象
        omsOrder.setModifyTime(new DateTime(System.currentTimeMillis()));
        omsOrder.setId(Long.valueOf(String.valueOf(moneyInfoParam.getOrderId())));
        omsOrderApi.updateReceiverInfo(omsOrder);
    }
}
